<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.user_name"
        clearable
        placeholder="用户名称"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
      />
      <el-input
        v-model="listQuery.controller"
        clearable
        placeholder="控制器名称"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
      />
      <el-input
        v-model="listQuery.method"
        clearable
        placeholder="方法名称"
        style="width: 200px"
        class="filter-item"
        @keyup.enter.native="handleFilter"
        @clear="handleFilter"
      />
      <el-date-picker
        v-model="listQuery.date_range"
        style="top: -4px"
        value-format="yyyy-MM-dd"
        type="daterange"
        unlink-panels
        range-separator="至"
        start-placeholder="开始日期"
        end-placeholder="结束日期"
        :picker-options="pickerOptions"
        @change="handleFilter"
      />
      <el-button
        v-waves
        style="margin-left: 10px"
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        type="danger"
        icon="el-icon-delete"
        @click="handleDelete(0)"
      >
        批量删除
      </el-button>
      <el-button
        class="filter-item"
        type="danger"
        icon="el-icon-delete"
        @click="handleDeleteAll()"
      >
        全部清空
      </el-button>
    </div>

    <el-table
      ref="itemDataTable"
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;margin-top: 20px"
      @sort-change="tableSortChange"
    >
      <el-table-column
        type="selection"
        width="55"
      />
      <el-table-column
        label="ID"
        prop="id"
        align="center"
        width="80"
      />
      <el-table-column label="用户名" align="center">
        <template slot-scope="{ row: { user_nameWrapper }}">
          <span v-html="user_nameWrapper" />
        </template>
      </el-table-column>
      <el-table-column label="控制器名称" align="center">
        <template slot-scope="{ row: { controllerWrapper }}">
          <span v-html="controllerWrapper" />
        </template>
      </el-table-column>
      <el-table-column label="方法名称" align="center">
        <template slot-scope="{ row: { methodWrapper }}">
          <span v-html="methodWrapper" />
        </template>
      </el-table-column>
      <el-table-column
        label="操作信息"
        prop="content"
        align="center"
      />
      <el-table-column
        label="ip地址"
        prop="ip_address"
        align="center"
        width="200"
      />
      <el-table-column
        label="操作日期"
        prop="create_time"
        sortable="custom"
        align="center"
        width="200"
      />
      <el-table-column label="操作" width="100" align="center" fixed="right">
        <template slot-scope="{ row: { id } }">
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="handleDelete(id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="handleFilter"
    />
  </div>
</template>

<script>
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import { wrapperKeyword } from '@/utils'
import { getSystemLogList, deleteSystemLog, clearSystemLog } from '@/api/system-log'

export default {
  name: 'SystemLogList',
  components: { Pagination },
  directives: { waves },
  data() {
    return {
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            picker.$emit('pick', [start, end])
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            picker.$emit('pick', [start, end])
          }
        }]
      },
      tableKey: 0,
      listLoading: false,
      total: 0,
      listQuery: {
        page: 1,
        limit: 20,
        user_name: '',
        controller: '',
        method: '',
        date_range: '',
        sort_prop: '',
        sort_order: ''
      },
      list: []
    }
  },
  watch: {
    list(val) {
      val.forEach(log => {
        log.user_nameWrapper = wrapperKeyword(this.listQuery['user_name'] || '', log.user_name)
        log.controllerWrapper = wrapperKeyword(this.listQuery['controller'] || '', log.controller)
        log.methodWrapper = wrapperKeyword(this.listQuery['method'] || '', log.method)
      })
    }
  },
  mounted() {
    this.getList()
  },
  methods: {
    handleFilter() {
      this.getList()
    },
    handleDelete(id) {
      this.$confirm('此操作将永久删除该数据，是否继续', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const ids = []
        // 判断是否为批量删除
        if (id === 0) {
          const _selection = this.$refs.itemDataTable.selection
          // console.log(_selection)
          if (_selection.length === 0) {
            this.$message({
              message: '请选择需要删除的数据',
              type: 'error'
            })
            return
          }
          _selection.forEach((value, index) => {
            ids.push(value.id)
          })
        } else {
          ids.push(id)
        }

        // console.log(ids)

        deleteSystemLog(ids).then(response => {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.getList()
        })
      }).catch(() => {})
    },
    handleDeleteAll() {
      this.$confirm('此操作将永久删除该数据，是否继续', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        clearSystemLog().then(response => {
          this.$message({
            message: response.msg,
            type: 'success'
          })
          this.getList()
        })
      }).catch(() => {})
    },
    tableSortChange(data) {
      const { prop, order } = data
      // console.log(prop, order, data)
      this.listQuery.sort_prop = prop
      this.listQuery.sort_order = order
      this.getList()
    },
    getList() {
      this.listLoading = true
      getSystemLogList(this.listQuery).then(response => {
        const { data, total } = response
        this.list = data
        this.total = total
        this.listLoading = false
      })
    }
  }
}
</script>

<style lang="scss" scoped>
</style>
